# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=connman
pkgver=1.43
pkgrel=0
pkgdesc="Daemon for managing internet connections"
url="https://git.kernel.org/pub/scm/network/connman/connman.git"
arch="all"
license="GPL-2.0-only"
depends="dbus"
makedepends="
	autoconf
	automake
	dbus-dev
	glib-dev
	libmnl-dev
	libnftnl-dev
	libtool
	openvpn
	ppp-dev
	readline-dev
	vpnc
	wpa_supplicant
	xl2tpd
	"
# For backward compatibility (Alpine <3.19).
provides="
	$pkgname-iptables=$pkgver-r$pkgrel
	$pkgname-nftables=$pkgver-r$pkgrel
	"
install="$pkgname.post-upgrade"
subpackages="
	$pkgname-dbg
	$pkgname-l2tp
	$pkgname-openvpn
	$pkgname-vpnc
	$pkgname-wireguard
	$pkgname-dev
	$pkgname-doc
	$pkgname-openrc
	"
source="https://www.kernel.org/pub/linux/network/connman/connman-$pkgver.tar.xz
	libresolv.patch
	dbus-rules.patch
	implicit.patch
	fix-implicit-basename.patch
	revert-rtnl-ignore-adding-removing-iface-to-bridge.patch
	gsupplicant-fix-asan-issue.patch
	openvpn.conf
	$pkgname.initd
	$pkgname.confd
	"

# secfixes:
#   1.41-r0:
#     - CVE-2022-23096
#     - CVE-2022-23097
#     - CVE-2022-23098
#   1.39-r0:
#     - CVE-2021-26675
#     - CVE-2021-26676

prepare() {
	default_prepare
	autoreconf -vif
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--disable-wispr \
		--enable-pie \
		--enable-iwd \
		--enable-nmcompat \
		--enable-wireguard \
		--enable-openvpn \
		--enable-l2tp \
		--enable-vpnc \
		--with-firewall=nftables
	make
}

check() {
	make check
}

package() {
	make install DESTDIR="$pkgdir"
	install -Dm644 src/main.conf "$pkgdir"/etc/$pkgname/main.conf

	install -Dm644 "$srcdir"/openvpn.conf -t "$pkgdir"/etc/$pkgname/vpn-plugin/

	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

l2tp() {
	pkgdesc="L2TP VPN plugin for ConnMan"
	depends="$pkgname=$pkgver-r$pkgrel xl2tpd"

	amove usr/lib/connman/plugins-vpn/l2tp.so
	amove usr/lib/connman/scripts/libppp-plugin.so
}

openvpn() {
	pkgdesc="OpenVPN plugin for ConnMan"
	depends="$pkgname=$pkgver-r$pkgrel openvpn"
	replaces="$pkgname"  # for backward compatibility

	amove etc/connman/vpn-plugin/openvpn.conf
	amove usr/lib/connman/plugins-vpn/openvpn.so
	amove usr/lib/connman/scripts/openvpn-script

	echo 'tun' | install -Dm644 /dev/stdin \
		"$subpkgdir"/usr/lib/modules-load.d/$subpkgname.conf
}

vpnc() {
	pkgdesc="IPsec (Cisco/Juniper) VPN plugin for ConnMan"
	depends="$pkgname=$pkgver-r$pkgrel vpnc"

	amove usr/lib/connman/plugins-vpn/vpnc.so
}

wireguard() {
	pkgdesc="WireGuard VPN plugin for ConnMan"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/connman/plugins-vpn/wireguard.so
}

sha512sums="
3289525cf764313700c520d3159431cdf719029096a1001d60ebdbda6406888699bc86336e7de890bf2c34a0624f3ba3081b0b57838a962b3c90bb5cd3484b38  connman-1.43.tar.xz
122b48fc9e25354e25ba3a3e0864bdd84da84457fed94aeea726bcb189b2f05f2cc361ae15f44af5c49bcee572e91e4c0488ef3b0bc79d20f6efe15853fb6b3a  libresolv.patch
0223dba51be67c131c297d0e865fe1276e4800be5eee25c4f142df8e651318bf5dae98c1d559bb70c002cdfc3ce3c24d12128ddca5656192f962d91af6906caa  dbus-rules.patch
e4095b51de42b31da6bc892e70be6dbcb34451348e90f50f1779c31a11a6b69013f50bafe156aacdfce4b49c2abdbd56cea62ecec20427bf2f6f942777f3c270  implicit.patch
0b9eb3231e8fa5a858403b6e3e3e3c1e88caf92a3899606278b48c37c7b3780b8d0c69156b8b09ea14804bb6824bcf89549d71a3ab8bd9358d761d198b9a2d39  fix-implicit-basename.patch
a20aeef104963d8150b97773921ef06e6157423bb7373556cd27009370d86e1fd2693160c3d0ff31c813255a74a84ab471489479ee5cada4630c564ea978b06f  revert-rtnl-ignore-adding-removing-iface-to-bridge.patch
8c5a17f562f443b3c35f9080088223451c23e51db1faadb3a812693ecdccdf29213e2f085998fbb2cde3c9df761b7b1dd45c461e4b6de88ac971d5a6a0c306f5  gsupplicant-fix-asan-issue.patch
7f90bfdbe27a468c401bde04ccedc7d15afba11ca460ee6155233c3ef99285033303cbf7afac3849cbcd6e95a90eae5bdc772e416aef57e5ada7c4eed38f4de9  openvpn.conf
f5cdeba7ebdbe52424ebe74088d57ff57d45dcdbfa6f17cfe7bfa59308c8188174e0f44a86ffaea8a7247793fbffc1463bbd791033e188f1f44f80847fbf71e0  connman.initd
73f505a777df04039ca51bc3959aa89969a0bbc21ea971fb98e04e9b5851553de35955453ed859f5a6273724bbcce38f13012950c6cb4e0a2bc80bc531ccff1e  connman.confd
"
